package com.vikutorika.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter("/index.jsp")
public class LoginFilter implements Filter {
        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
                System.out.println("LoginFilter");
                HttpServletRequest request = (HttpServletRequest) servletRequest;
                HttpServletResponse response = (HttpServletResponse) servletResponse;
//                String uri = request.getRequestURI();
//                if (uri.endsWith("/login.html") || uri.startsWith("/lightlite/user"))
//                        filterChain.doFilter(servletRequest, servletResponse);
                HttpSession session = request.getSession();
                Object user = session.getAttribute("userInfo");
                if(user == null)
                        response.sendRedirect("/Vikutorika/login.html");
                filterChain.doFilter(servletRequest, servletResponse);
        }
}
